﻿@using KimPhongCRM.Lib
@model List<KimPhongCRM.Models.sp_Select_Notify_Result>
@{ Dictionary<string, string> dicTable = libDictionary.dicIconTable(); }

<li class="dropdown hidden-phone">
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
        <i class="font-icon-bell-alt  icon-white"></i>
        <span class="label label-important hidden-phone">@Model.Count(n => n.loaiSuaDoi == -1)</span>
        <span class="label label-success hidden-phone">@Model.Count(n => n.loaiSuaDoi == 1)</span>
        <span class="label label-warning hidden-phone">@Model.Count(n => n.loaiSuaDoi == 0)</span>
    </a>
    <ul class="dropdown-menu notifications">
        <li>
            <span class="dropdown-menu-title">Bạn có @Model.Count() thông báo mới</span>
        </li>
        <div class="notify_content">
            @foreach (var value in Model)
            {
                int edit = value.loaiSuaDoi;
                if (edit == -1)
                {
                <li class="warning notify" id="notify_@{@value.notify_id}">
                    <a id="notify-click" href="@Url.Action("PopupDetail", "Notify", new { malichsu = @value.maLichSu, tablename = @value.table_name })">
                        <i class="@if (dicTable.ContainsKey(value.table_name.ToString())) { Write(dicTable[value.table_name.ToString()]); }" style="color:#FA5833"></i>
                        <span class="message">
                            @{string noidung = value.noiDung.Replace("\n", ". ");}
                            @Html.Raw(stringUtils.trimStr(noidung, 25))
                        </span>
                        @{long rel = libTimer.ToJsTimeTicks(value.ngaySua);}
                        @{long cur = libTimer.ToJsTimeTicks(DateTime.UtcNow);}
                        <span class="time" cur="@cur" rel="@rel">1 min</span>
                    </a>
                </li>
                }
                else if (edit == 0)
                { 
                <li class="update notify" id="notify_@{@value.notify_id}">
                    <a id="notify-click" href="@Url.Action("PopupDetail", "Notify", new { malichsu = @value.maLichSu, tablename = @value.table_name })">
                        <i class="@if (dicTable.ContainsKey(value.table_name.ToString())) { Write(dicTable[value.table_name.ToString()]); }" style="color:#F4A506"></i>
                        <span class="message">
                            @{string noidung = value.noiDung.Replace("\n", ". ");}
                            @Html.Raw(stringUtils.trimStr(noidung, 25))
                        </span>
                        @{long rel = libTimer.ToJsTimeTicks(value.ngaySua);}
                        @{long cur = libTimer.ToJsTimeTicks(DateTime.UtcNow);}
                        <span class="time" cur="@cur" rel="@rel">1 min</span>
                    </a>
                </li>
                }
                else
                {
                <li class="addnew notify" id="notify_@{@value.notify_id}">
                    <a id="notify-click" href="@Url.Action("PopupDetail", "Notify", new { malichsu = @value.maLichSu, tablename = @value.table_name })">
                        <i class="@if (dicTable.ContainsKey(value.table_name.ToString())) { Write(dicTable[value.table_name.ToString()]); }" style="color:#5AAD34"></i>
                        <span class="message">
                            @{string noidung = value.noiDung.Replace("\n", ". ");}
                            @Html.Raw(stringUtils.trimStr(noidung, 25))
                        </span>
                        @{long rel = libTimer.ToJsTimeTicks(value.ngaySua);}
                        @{long cur = libTimer.ToJsTimeTicks(DateTime.UtcNow);}
                        <span class="time" cur="@cur" rel="@rel">1 min</span>
                    </a>
                </li>
                }
            }
        </div>
        <li>
            <a href="@Url.Action("Index","Notify")" class="dropdown-menu-sub-footer">Xem tất cả các thông báo</a>
        </li>
    </ul>
</li>
<script>
    $(document).ready(function () {
        //$('li.notify span.time').countdown({
        //    since: $(this).attr('rel'), format: 'HMS', description: ''
        //});
        $('li.notify span.time').each(function (i, item) {
            var rel = $(item).attr('rel');
            rel = parseInt(rel);
            var cur = parseInt($(item).attr('cur'));
            var diff = cur - rel;
            var newdate = new Date(rel);
            var format = 'S';
            var layout = '{sn} {sl}';
            if (diff >= 86400000) {
                format = 'D';
                layout = '{dn} {dl}';
            } else if (diff >= 3600000) {
                format = 'H';
                layout = '{hn} {hl}';
            } else if (diff >= 60000) {
                format = 'M';
                layout = '{mn} {ml}';
            }

            $(item).countdown({ since: newdate, format: format, layout: layout });
        });

    });
</script>
